Questions
29 of 30
1What is the purpose of the transform property in CSS?
2What are the different types of CSS transformations?
3What does transform: translate() do?
4What is the difference between translateX() and translateY()?
5How does rotate() work in CSS?
6What is the unit used in rotation?
7What is the difference between scale() and scaleX(), scaleY()?
8What happens when you use negative values in scale()?
9What does the skew() function do?
10Can you combine multiple transform functions together? How? What is the use of the transform-origin property?
11What is the default transform-origin value?
12How does changing transform-origin affect rotation or scaling?
13What is the difference between translate() and using position or margin for movement?
14What is the difference between 2D and 3D transformations?
15What is the difference between 2D and 3D transformations?
16How do you apply a 3D rotation using CSS?
17How do you apply a 3D rotation using CSS?
18What are perspective and perspective-origin in 3D transforms? What is the purpose of transform-style: preserve-3d?
19What is the use of matrix() in CSS transforms?
20What’s the difference between matrix() and matrix3d()?
21How can you center an element using transform and translate()?
22What is the transition property in CSS used for?
23What are the four main components of a transition?
24What does transition-duration specify?
25What are the common timing function values (e.g., ease, linear, ease-in)?
26What is the shorthand syntax for transition?
27Can multiple CSS properties be transitioned simultaneously? How does transition differ from animation in CSS?
28Can you apply a transition to an element’s pseudo-class (like :hover)?
29Can transitions be triggered by JavaScript?
30What happens if transition-duration is set to 0s?
29 / 30

Can transitions be triggered by JavaScript?

Triggering CSS Transitions with JavaScript

Yes, transitions can be triggered by JavaScript. While CSS transitions usually respond to user interactions like :hover or :focus, you can also use JavaScript to change a property dynamically — when the property value changes, the transition runs automatically if defined in CSS.

You define the transition in your CSS, and then use JavaScript to modify the element’s style or class. When the property value changes, the browser applies the transition smoothly.

Example: Trigger Transition with JavaScript

When the user clicks the box, JavaScript toggles the expanded class, causing the width and background color to transition smoothly.

You can also apply transitions by changing inline styles directly with JavaScript. As long as the element has a transition defined, any property changes will animate smoothly.

Example: Inline Style Change

Here, the transform property is updated directly through JavaScript, and because a transition is set, it animates smoothly.

Best Practices
  1. 1

    Define transitions in CSS for cleaner separation between style and logic.

  2. 2

    Use JavaScript to trigger state changes by toggling classes or updating styles.

  3. 3

    Transitions only occur when a property’s value actually changes.

  4. 4

    JavaScript can control when, how, and under what conditions transitions happen.

Use JavaScript-triggered transitions when you need fine-grained control, such as animating elements after a delay, in response to user input, or after loading dynamic content.

Difficulty: 5/10
Topics: CSS transitions, JavaScript DOM manipulation, performance

Scenario Questions

0-2 years experience
  1. 1

    How would you use JavaScript to start a CSS transition on a button when it's clicked?

  2. 2

    If you add a class that defines a transition after the element is already rendered, what happens?

  3. 3

    Will a transition run if you set the transition property via JavaScript after you already changed the element's style? Why or why not?

2-5 years experience
  1. 1

    We have a modal that should fade in using a CSS transition, but the animation sometimes skips on slower devices. Walk me through how you'd trigger the transition with JavaScript and what you'd check to debug the issue.

  2. 2

    You need to toggle a menu's height with a smooth transition, but the height is dynamic. How would you implement this using JS to trigger the transition, and what trade‑offs exist?

  3. 3

    Why might a transition not fire when you add a class via JavaScript inside a requestAnimationFrame callback? Explain the timing considerations.

5-8 years experience
  1. 1

    You're building a component library where every component can be animated via CSS transitions triggered by JS. How would you design the API to ensure consistent behavior, avoid layout thrashing, and keep the bundle size low?

  2. 2

    In a large single‑page app, many elements are added and removed dynamically, each with entry/exit transitions. How would you manage JS‑triggered transitions to prevent memory leaks and ensure smooth performance?

  3. 3

    Explain how you would coordinate JavaScript‑driven transitions with the prefers‑reduced‑motion media query across the whole application.

8+ years experience
  1. 1

    Our legacy product mixes inline style changes with CSS transition classes, causing flicker and inconsistent animations. As a staff engineer, outline a migration plan to centralize transition handling, including how you'd phase out JS‑triggered transitions, enforce design‑system constraints, and measure performance impact.

  2. 2

    When designing a cross‑team UI framework, how would you decide whether to expose transition control via JavaScript APIs versus pure CSS, considering accessibility, theming, and future platform changes?

  3. 3

    Describe the architectural considerations for supporting server‑side rendering of components that rely on JS‑triggered CSS transitions, ensuring hydration does not cause unwanted animations.

Follow-up Questions

  • What would you do if the transition never fires?
  • How do you make sure the animation respects a user's reduced‑motion preference?
  • Are there any risks when triggering transitions inside a requestAnimationFrame loop?